package com.fit.pages.hotel;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.services.PropertyAccess;

import com.fit.annotations.AdminAccess;
import com.fit.annotations.UserAccess;
import com.fit.dao.destination.DestinationDAO;
import com.fit.dao.hotel.HotelDAO;
import com.fit.entities.Destination;
import com.fit.entities.Hotel;
import com.fit.model.glov.GenericSelectModel;
import com.fit.pages.ErrorPage;

@AdminAccess
@UserAccess
public class InsertHotel {

	@Property
	private Hotel hotel;

	@InjectPage
	private ErrorPage errorPage;

	@Inject
	private HotelDAO hotelDAO;

	@Inject
	private PropertyAccess access;

	@Property
	private Destination destination;

	@Inject
	private DestinationDAO destinationDAO;

	private GenericSelectModel<Destination> destinations;

	public GenericSelectModel<Destination> getDestinations() {
		destinations = new GenericSelectModel<Destination>(
				destinationDAO.findAll(), Destination.class, "city", "id",
				access);
		return destinations;
	}

	Object onSuccess() {
		errorPage.setMessage("");

		hotel.setDestination(destination);
		if (hotelDAO.save(hotel)) {
			errorPage.setMessage("Hotel successfully added!");
			return errorPage;
		}
		errorPage.setMessage("Hotel is null!");
		return errorPage;

	}

}
